package org.zjx.action;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.SneakyThrows;

import java.io.InvalidClassException;

@Data
public class BaseAction implements IAction{
    protected ActionType actionType;
    protected long timestamp;

    public BaseAction(){
        timestamp = System.currentTimeMillis();
    }

    @Override
    public ActionType getActionType() {
        return actionType;
    }

    @Override
    public long getTimestamp() {
        return timestamp;
    }

    @Override
    public JSONObject toJson() {
        JSONObject json = new JSONObject();
        json.put("actionType", actionType.name());
        json.put("timestamp", timestamp);
        return json;
    }

    @Override
    @SneakyThrows
    public void validate() {
        // 基础验证：指令有效期（5秒内有效）
        if (System.currentTimeMillis() - timestamp > 5000) {
            throw new InvalidClassException("指令已过期");
        }
    }
}
